package com.jl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.constant.Constants;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.utils.MathMethodUtil;
import com.jl.mapper.DamageComponentMapper;
import com.jl.model.DamageComponent;
import com.jl.service.IDamageComponentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 损坏(更换)零件表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Service
public class DamageComponentServiceImpl extends ServiceImpl<DamageComponentMapper, DamageComponent> implements IDamageComponentService {

    @Override
    public void selectDamageComponentDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<DamageComponent> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public void selectEasyDamageComponent(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<DamageComponent> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectEasyDamageDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public void selectDamageComponentByDeviceId(PageDataInfo<Map<String, Object>> pageDataInfo, Long deviceId) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDamageComponentByDeviceId(page,deviceId);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public Integer getDeviceComponentAvgLife(Long deviceId,Long componentId) {
        List<Integer> integerList = baseMapper.selectDeviceComponentLifeWithComId(deviceId,componentId);
        if(integerList.isEmpty()){
            return 0;
        }
        Integer median = MathMethodUtil.getMedian(integerList);
        return median;
    }

    @Override
    public void getTopDamageComponent(PageDataInfo<Map<String, Object>> pageDataInfo, Integer type, String startTime, String endTime) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(Constants.DEVICE_RUN_STATUS_WEEK_TYPE.equals(type)){
//            周
            mapList = baseMapper.selectTopDamageComponentCount(type, startTime, null);
        }else if(Constants.DEVICE_RUN_STATUS_MONTH_TYPE.equals(type)){
//            月
            mapList = baseMapper.selectTopDamageComponentCount(type, startTime, endTime);
        }
        pageDataInfo.setTotal(mapList.size());
        pageDataInfo.setRecords(mapList);
    }

}
